<?php
class AccountController extends FrontEndController
{
    public function filters()
    {
        return array(
            'accessControl',
        );
    }

    /**
     * Specifies the access control rules.
     * This method is used by the 'accessControl' filter.
     *
     * @return array access control rules
     */
    public function accessRules()
    {
        return array(
            array('allow', 'actions' => array('create'),
                'roles' => array('vip')
            ),
            array('allow', 'actions' => array('logout'),
                'roles' => array('member')
            ),
            array('allow', 'actions' => array('register','login'),
                'users' => array('*')
            ),
            array('deny', 'users' => array('*')
            )
        );
    }

    public function actionRegister()
    {
        if (Yii::app()->user->id != null) {
            $this->redirect(Yii::app()->homeUrl);
        } else {
            $model = new ThanhVien();
            if (isset($_POST['ThanhVien'])) {
                $model->attributes = $_POST['ThanhVien'];
                if ($model->exists('ten_tai_khoan=:ten_tai_khoan', array(':ten_tai_khoan' => $model->ten_tai_khoan))) {
                    $model->addError('ten_tai_khoan', 'Tên tài khoản đã được đăng ký.');
                } elseif ($model->exists('email=:email', array(':email' => $model->email))) {
                    $model->addError('email', 'Email đã được đăng ký.');
                } else {
                    $password = $model->mat_khau;
                    if ($model->save()) {
                        $login = new LoginForm('Front');
                        $login->username = $model->ten_tai_khoan;
                        $login->password = $password;
                        if ($login->validate() && $login->login()) {
                            $this->redirect(array('site/index'));
                        }
                    }
                }
            }
            $this->render('register', array(
                'model' => $model
            ));
        }
    }

    /**
     * Displays the login page
     */
    public function actionLogin()
    {
        if (Yii::app()->user->id != null) {
            $this->redirect(Yii::app()->homeUrl);
        } else {
            $model = new LoginForm();
            $model->loginType = 'Front';

            // if it is ajax validation request
            if (isset($_POST['ajax']) && $_POST['ajax'] === 'login-form') {
                echo CActiveForm::validate($model);
                Yii::app()->end();
            }

            // collect user input data
            if (isset($_POST['LoginForm'])) {
                $model->attributes = $_POST['LoginForm'];
                // validate user input and redirect to the previous page if valid
                if ($model->validate() && $model->login())
                    $this->redirect(Yii::app()->user->returnUrl);
            }
            // display the login form
            $this->render('login', array('model' => $model));
        }
    }

    /**
     * Logs out the current user and redirect to homepage.
     */
    public function actionLogout()
    {
        Yii::app()->user->logout();
        $this->redirect(Yii::app()->homeUrl);
    }
}